package com.bawei.a2010douyin.ui.zhibo;

import android.icu.number.Scale;
import android.view.animation.AnimationSet;

import com.bawei.a2010douyin.base.mvp.BasePresenter;
import com.bawei.a2010douyin.base.rx.BaseFunc;
import com.bawei.a2010douyin.entity.DanmakuEntity;
import com.bawei.a2010douyin.entity.GiftEntity;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ZhiBoPresenter extends BasePresenter<ZhiBoContract.IZhiBoView> implements ZhiBoContract.IZhiBoPresenter {
    private ZhiBoContract.IZhiBoModel mIZhiBoModel;
    public ZhiBoPresenter() {
        mIZhiBoModel = new ZhiBoModel();
    }

    @Override
    public void gift() {
        mIZhiBoModel.gift()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<List<GiftEntity>>())
                .subscribe(new Observer<List<GiftEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(List<GiftEntity> giftEntities) {
                        if(isAttach()){
                            getView().showGift(giftEntities);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getDanMus(int videoId) {
        mIZhiBoModel.getDanMus(videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<List<DanmakuEntity>>())
                .subscribe(new Observer<List<DanmakuEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(List<DanmakuEntity> danmakuEntities) {
                        if(isAttach()){
                            getView().showDanmus(danmakuEntities);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
